package cn.jolyoulu.adminweb.service.impl;

import cn.jolyoulu.adminweb.entity.bo.query.SysLoginLogQueryBO;
import cn.jolyoulu.adminweb.entity.bo.query.SysOperateLogQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysLoginLogVO;
import cn.jolyoulu.adminweb.entity.vo.SysOperateLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.jolyoulu.adminweb.service.ApiSysLogService;
import cn.jolyoulu.cmnservice.entity.po.SysLoginLog;
import cn.jolyoulu.cmnservice.entity.po.SysOperateLog;
import cn.jolyoulu.cmnservice.entity.po.SysUser;
import cn.jolyoulu.cmnservice.serivce.SysLoginLogService;
import cn.jolyoulu.cmnservice.serivce.SysOperateLogService;
import cn.jolyoulu.cmnservice.serivce.SysUserService;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LuZhouJin
 * @Date 2023/3/22
 */
@Slf4j
@Service
public class ApiSysLogServiceImpl implements ApiSysLogService {

    @Autowired
    private SysLoginLogService sysLoginLogService;

    @Autowired
    private SysOperateLogService sysOperateLogService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public JlPage<SysLoginLogVO> loginPageList(SysLoginLogQueryBO bo) {
        return JlPage.execute(bo.getPage(), bo.getPageSize(), () -> {
            LambdaQueryWrapper<SysLoginLog> ew = new LambdaQueryWrapper<>();
            ew.orderByDesc(SysLoginLog::getLoginTime);
            List<SysLoginLog> list = sysLoginLogService.list(ew);
            return list.stream().map(SysLoginLogVO::new).collect(Collectors.toList());
        });
    }

    @Override
    public SysLoginLogVO getLoginById(String id) {
        SysLoginLog sysLoginLog = sysLoginLogService.getById(id);
        return new SysLoginLogVO(sysLoginLog);
    }

    @Override
    @Transactional
    public void delLoginByIds(List<String> ids) {
        for (String id : ids) {
            sysLoginLogService.removeById(id);
        }
    }

    @Override
    public JlPage<SysOperateLogVO> operatePageList(SysOperateLogQueryBO bo) {
        return JlPage.execute(bo.getPage(), bo.getPageSize(), () -> {
            LambdaQueryWrapper<SysOperateLog> ew = new LambdaQueryWrapper<>();
            ew.select(SysOperateLog::getId,
                    SysOperateLog::getUserId,
                    SysOperateLog::getReqMethod,
                    SysOperateLog::getReqUrl,
                    SysOperateLog::getOperateLocation,
                    SysOperateLog::getOperateIp,
                    SysOperateLog::getStatus,
                    SysOperateLog::getCostTime,
                    SysOperateLog::getOperateTime);
            ew.orderByDesc(SysOperateLog::getOperateTime);
            List<SysOperateLog> list = sysOperateLogService.list(ew);
            return list.stream().map(item -> {
                SysOperateLogVO logVO = new SysOperateLogVO(item);
                SysUser user = sysUserService.getByIdIgnDelFlag(item.getUserId());
                logVO.setNickname(user.getNickname());
                return logVO;
            }).collect(Collectors.toList());
        });
    }

    @Override
    public SysOperateLogVO getOperateById(String id) {
        SysOperateLog sysOperateLog = sysOperateLogService.getById(id);
        return new SysOperateLogVO(sysOperateLog);
    }

    @Override
    @Transactional
    public void delOperateByIds(List<String> ids) {
        for (String id : ids) {
            sysOperateLogService.removeById(id);
        }
    }
}
